using System;
using System.Web;
using Microsoft.Web.Infrastructure.DynamicModuleHelper;
using Ninject;
using Ninject.Modules;
using Ninject.Web.Common;
using WCF.NinjectDependencyInjectionDemo;
using WCF.NinjectDependencyInjectionDemo.Config;

[assembly: WebActivator.PreApplicationStartMethod(typeof(App), "PreApplicationStart")]
[assembly: WebActivator.ApplicationShutdownMethodAttribute(typeof(App), "ApplicationStop")]

namespace WCF.NinjectDependencyInjectionDemo
{
    public static class App
    {
        #region Properties
        private static readonly Bootstrapper bootstrapper = new Bootstrapper();
        #endregion

        #region Application Start and Stop methods
        public static void PreApplicationStart() 
        {
            //Ninject
            DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
            DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
            bootstrapper.Initialize(CreateNinjectKernel);

            //Add more here if needed
        }
        
        public static void ApplicationStop()
        {
            //Ninject
            bootstrapper.ShutDown();

            //Add more here if needed
        }
        #endregion

        #region Utility Methods
        private static IKernel CreateNinjectKernel()
        {
            var modules = new INinjectModule[]
            {
                new NinjectConfig()
            };

            var kernel = new StandardKernel(modules);
            kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
            kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();
            return kernel;
        }
        #endregion
    }
}
